import 'package:flutter/material.dart';
import 'package:hktk_app/dao/industry_dao.dart';
import 'package:hktk_app/model/industry/category_model.dart';
import 'package:hktk_app/pages/subject/child/question_detail_page.dart';
import 'package:hktk_app/util/event_bus_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/chapter_title2_widget.dart';
import 'package:hktk_app/widget/page_title_widget.dart';

/// 历年真题页面
class TrueQuestionPage extends StatefulWidget {
  /// 标题名称
  final String title;

  const TrueQuestionPage({super.key, required this.title});

  @override
  State<TrueQuestionPage> createState() => _TrueQuestionPageState();
}

class _TrueQuestionPageState extends State<TrueQuestionPage>
    with TickerProviderStateMixin {
  /// 对应章节数据
  late List<CategoryModel> _categoryList;

  /// tab控制器
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    // 重缓存中获取选择的科目
    _categoryList = IndustryDao.getCategoryModelsSel();
    // 获取选中下标
    int index = IndustryDao.getSelectCategoryId() ?? 0;
    _tabController = TabController(
      length: _categoryList.length,
      vsync: this,
      initialIndex: index,
    );
  }

  @override
  void dispose() {
    super.dispose();
    int index = _tabController.index;
    eventBus.fire(EventCategoryTitle(index: index, list: _categoryList));
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 获取安全边距
    var bottom = MediaQuery.of(context).padding.bottom;
    return Scaffold(
      backgroundColor: colorWhite2,
      body: Padding(
        padding: EdgeInsets.only(bottom: bottom),
        child: Column(
          children: [
            PageTitleWidget(
              name: widget.title,
              pageContext: context,
              rightFun: _checkTitle,
            ),
            hiSpace(height: 30),
            ChapterTitle2Widget(
              tabController: _tabController,
              subList: _categoryList,
              pageName: "/paper-library",
              arguments: {
                "title": widget.title,
              },
            ),
            hiSpace(height: 30),
            Expanded(child: _content2()),
          ],
        ),
      ),
    );
  }

  /// 标题垃圾桶被点击方法
  _checkTitle() {

  }

  /// 内容显示方法
  _content2() {
    return TabBarView(
      controller: _tabController,
      children: _categoryList
          .map((CategoryModel model) => QuestionDetailPage(
                subId: model.id!,
                columnName: widget.title,
              ))
          .toList(),
    );
  }
}
